package com.qiguliuxing.dts.merchant.controller;

import com.qiguliuxing.dts.common.constant.ApiAdminConsts;
import com.qiguliuxing.dts.common.constant.LogParamsConsts;
import com.qiguliuxing.dts.common.util.ResponseUtils;
import com.qiguliuxing.dts.core.annotation.aoplog.WebLog;
import com.qiguliuxing.dts.core.validator.Order;
import com.qiguliuxing.dts.core.validator.Sort;
import com.qiguliuxing.dts.merchant.annotation.RequiresPermissionsDesc;
import com.qiguliuxing.dts.merchant.service.MerchantBillService;
import com.qiguliuxing.dts.merchant.support.AuthSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 商户端账单
 *
 * @author qiguliuxing
 */
@RestController
@RequestMapping("/merchant/withdraw")
@Validated
@Api(tags = ApiAdminConsts.MerchantWithdraw.PARENT)
public class MerchantAccountTraceController {
    private static final Logger logger = LoggerFactory.getLogger(MerchantAccountTraceController.class);

    @Autowired
    private MerchantBillService merchantBillService;

    /**
     * 查询流水列表
     *
     * @param page        页码
     * @param limit       分页大小
     * @param sort        排序字段
     * @param order       排序规则
     * @param accountNo   提现账号
     * @param statusArray 状态数组
     * @param time        时间数组
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("merchant:bill:list")
    @RequiresPermissionsDesc(menu = {"财务管理", "账单管理"}, button = "查询")
    @ApiOperation(value = ApiAdminConsts.MerchantWithdraw.LIST)
    @WebLog(description = ApiAdminConsts.MerchantWithdraw.LIST, excludes = {LogParamsConsts.RESPONSE})
    public Object list(@ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
                       @ApiParam(value = "分页大小", defaultValue = "10") @RequestParam(defaultValue = "10") Integer limit,
                       @ApiParam(value = "排序字段", defaultValue = "add_time") @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @ApiParam(value = "排序规则", defaultValue = "desc") @Order @RequestParam(defaultValue = "desc") String order,
                       @ApiParam(value = "提现账号") @RequestParam(required = false) String accountNo,
                       @ApiParam(value = "状态数组") @RequestParam(required = false) List<Byte> statusArray,
                       @ApiParam(value = "时间数组") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") List<LocalDateTime> time) {
        Integer brandId = AuthSupport.merchantBrandId();
        if (brandId == null) {
            logger.error("查询提现记录出错，当前登录用户商户为空！");
            return ResponseUtils.serious();
        }
        return merchantBillService.listWithdraw(page, limit, sort, order, brandId, accountNo, statusArray, time);
    }
}
